<template>
  <div class="data-table-container layout-padding">
    <!-- 数据列表展示 -->
    <el-card class="layout-padding-auto" shadow="hover">
      <el-form ref="queryFormRef" :model="state.tableData.queryParams.params" inline>
        <el-form-item class="!mr-4" prop="tenantName">
          <el-input v-model="state.tableData.queryParams.params.tenantName" class="max-w-180px" clearable
                    placeholder="请输入企业名称"
                    size="default" @keyup.enter.native="getTableData">
          </el-input>
        </el-form-item>
        <el-form-item class="!mr-4">
          <el-button v-auth="'tenant.list'" size="default" type="primary" @click="getTableData">
            <el-icon>
              <ele-Search/>
            </el-icon>
            查询
          </el-button>
        </el-form-item>
        <el-form-item class="!mr-4">
          <el-button v-auth="'tenant.add'" size="default" type="success" @click="openDialog('add')">
            <el-icon>
              <ele-FolderAdd/>
            </el-icon>
            新增
          </el-button>
        </el-form-item>
        <el-form-item class="!mr-4">
          <el-button size="default" type="primary" @click="onHandleResetFields">
            <el-icon>
              <ele-Refresh/>
            </el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="state.tableData.loading" :data="state.tableData.data" style="width: 100%">
        <el-table-column label="序号" type="index" width="60"/>
        <el-table-column label="企业名称" prop="tenantName" show-overflow-tooltip></el-table-column>
        <el-table-column label="联系人" prop="contact" show-overflow-tooltip></el-table-column>
        <el-table-column label="地址信息" prop="address" show-overflow-tooltip></el-table-column>
        <el-table-column label="联系电话" prop="phone" show-overflow-tooltip></el-table-column>
        <el-table-column label="企业状态" prop="active" show-overflow-tooltip="">
          <template #default="scope">
            <el-tag v-if="scope.row.active" type="success">启用</el-tag>
            <el-tag v-else type="info">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="描述" prop="remarks" show-overflow-tooltip></el-table-column>
        <el-table-column label="创建时间" prop="createTime" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" text type="primary" @click="openDialog('edit', scope.row)">修改</el-button>
            <el-button size="small" text type="danger" @click="onRowDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination v-model:current-page="state.tableData.queryParams.pageNum"
                     v-model:page-size="state.tableData.queryParams.pageSize" :page-sizes="[10, 20, 30]"
                     :pager-count="5"
                     :total="state.tableData.total" background class="mt15"
                     layout="total, sizes, prev, pager, next, jumper"
                     @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange">
      </el-pagination>
    </el-card>
    <TenantDialog ref="tenantDialogRef" @refresh="getTableData()"/>
  </div>
</template>
<script lang="ts" name="systemTenant" setup>
import {defineAsyncComponent, onMounted, reactive, ref} from 'vue';
import type {TenantState, TenantType} from '/@/api/sys/types'
import {tenantApi} from '/@/api/sys/tenant'
import {ElMessage, ElMessageBox} from 'element-plus';

const TenantDialog = defineAsyncComponent(() => import('/@/views/system/tenant/dialog.vue'))

const tenantDialogRef = ref();
const queryFormRef = ref();

// 数据对象
const state = reactive<TenantState>({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      params: {
        tenantName: ''
      }
    }
  }
})

// 加载表格数据
const getTableData = () => {
  state.tableData.loading = true;
  tenantApi()
      .getTableData(state.tableData.queryParams)
      .then((res) => {
        state.tableData.data = res.data.records;
        state.tableData.total = res.data.total;
        state.tableData.loading = false;
      })
      .catch(() => {
        state.tableData.loading = false;
      });
};
// 重置查询
// 打开弹窗
const openDialog = (type: string, row?: TenantType) => {
  tenantDialogRef.value.openDialog(type, row);
}
// 删除数据
const onRowDel = (row: TenantType) => {
  ElMessageBox.confirm(`此操作将永久删除企业名称：“${row.tenantName}”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
      .then(() => {
        tenantApi()
            .delRecord(row)
            .then(() => {
              getTableData();
              ElMessage.success('删除成功');
            });
      })
      .catch(() => {
      });
};
// 页码改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.queryParams.pageNum = val;
  getTableData();
};
// 页数改变
const onHandleSizeChange = (val: number) => {
  state.tableData.queryParams.pageSize = val;
  getTableData();
};
// 页面加载
onMounted(() => {
  getTableData();
});

// 重置查询表单
const onHandleResetFields = () => {
  queryFormRef.value.resetFields();
  getTableData();
};
</script>
